package com.cssw.fyzb.core.dao.acct;

import com.cssw.fyzb.core.model.dodb.GzAddPrice;
import com.cssw.fyzb.core.model.dodb.TeamGzPrice;
import com.cssw.fyzb.core.util.TimeUtil;
import com.ydn.dbframe.plugin.activerecord.Db;
import com.ydn.dbframe.plugin.activerecord.Page;
import com.ydn.dbframe.plugin.activerecord.parse.SqlParse;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: yangyiqun
 * @Date: 2021/1/7 17:33
 */
@Repository
public class GzAddPriceDaoImpl implements GzAddPriceDao {

    @Override
    public GzAddPrice getTodayVipAddPrice(Long userId) {
        return getTodayGzAddPrice(userId, GzAddPrice.TASK_TYPE_VIP);
    }

    @Override
    public GzAddPrice getTodayGzAddPrice(Long userId, String taskType) {
        Integer dayTime = TimeUtil.getTimeYYMMDD(new Date());
        StringBuffer sql = new StringBuffer();
        sql.append("select * from t_gz_add_price where user_id = ? and day_time = ? and task_type = ? ");
        return GzAddPrice.dao.findFirst(sql.toString(), userId, dayTime, taskType);
    }

    @Override
    public BigDecimal sumTodayAddPriceByTaskType(Long userId, String taskType) {
        Integer dayTime = TimeUtil.getTimeYYMMDD(new Date());
        StringBuffer sql = new StringBuffer();
        sql.append("select sum(price) from t_gz_add_price where user_id = ? and day_time = ? and task_type = ? ");
        BigDecimal sumPrice = Db.queryBigDecimal(sql.toString(), userId, dayTime, taskType);
        if (null == sumPrice){
            sumPrice = BigDecimal.ZERO;
        }
        return sumPrice;
    }

    @Override
    public Long save(Long userId, BigDecimal price, String taskType, String tradeId, String itemName) {
        GzAddPrice gzAddPrice = new GzAddPrice();
        Integer dayTime = TimeUtil.getTimeYYMMDD(new Date());
        gzAddPrice.setUserId(userId);
        gzAddPrice.setDayTime(dayTime);
        gzAddPrice.setCreateTime(new Date());
        gzAddPrice.setPrice(price);
        gzAddPrice.setTaskType(taskType);
        gzAddPrice.setTradeId(tradeId);
        gzAddPrice.setItemName(itemName);
        gzAddPrice.save();
        return gzAddPrice.getId();
    }

    @Override
    public BigDecimal getAddPriceToday(Long userId) {
        Date now = new Date();
        int dayTime = TimeUtil.getTimeYYMMDD(now);
        String sql = "select sum(price) as num from t_gz_add_price \n" +
                " where user_id = ? and day_time =?";
        BigDecimal res = Db.queryBigDecimal(sql, userId, dayTime);
        return res == null ? new BigDecimal(0) : res;
    }

    @Override
    public Page<GzAddPrice> getGzAddPrice(Long userId) {
        Map param = new HashMap<>();
        param.put("userId", userId);
        SqlParse sqlParse = new SqlParse(param);
        sqlParse.addSQL(" select * from t_gz_add_price ");
        sqlParse.addSQL(" where user_id = :userId ");
        sqlParse.addSQL(" order by create_time desc ");
        return GzAddPrice.dao.paginate(1, 30, sqlParse);
    }

    @Override
    public void updateGzAddPrice(Long id, BigDecimal price) {
        GzAddPrice gzAddPrice = new GzAddPrice();
        gzAddPrice.setId(id);
        gzAddPrice.setPrice(price);
        gzAddPrice.update();
    }
}
